//
//  CallStarViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "CallStarViewController.h"
#import "CallStarLeavewordViewController.h"
#import "CallStarCell.h"
#import "CallStarModel.h"
#import "AddStarView.h"
#import "RechargeListModel.h"

@interface CallStarViewController ()<UITableViewDelegate,UITableViewDataSource,CallStarCellDelegate,AddStarViewDelegate>{
    NSMutableArray *_callStarArray;
    NSMutableArray *_rechargeListArray;
}
/**
   选择兑换类型
 */
@property (nonatomic, assign) int tag;
@property (nonatomic, strong) UITableView *tableView;
/**
  呼唤明星最大数
 */
@property (nonatomic, assign) int MaxNum;
@property (nonatomic, strong) AddStarView *addStarView;
@property (nonatomic, copy) NSString *balance;
@end

@implementation CallStarViewController

#pragma mark -- 呼唤明星
- (void)CallStarRequestWithShoutID:(NSString *)shoutId IndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"shout":shoutId
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kCallStar];
    [self showHUDInView:self.view title:nil];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        [self hideHUD];
        if ([responseObject[@"code"] intValue] == 200) {
            CallStarModel *model = _callStarArray[indexPath.row];
            model.booked = responseObject[@"data"][@"status"];
            model.s_number = responseObject[@"data"][@"number"];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        } else if ([responseObject[@"code"] intValue] == 400) { //次数不足
            [self reChargeListRequest];
        }
    } failureBlock:^(NSError *error) {
       [self hideHUD];
    } showHUD:NO];
}

#pragma mark -- 呼唤明星列表网络请求
- (void)CallStarListRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kCallStarList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *dic in array) {
                CallStarModel *model = [[CallStarModel alloc] initWithDict:dic];
                [_callStarArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

//兑换加星次数列表
- (void)reChargeListRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"type":@"250"
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kGifList];\
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            self.balance = responseObject[@"data"][@"u_balance"];
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *rechargeDict in array) {
                RechargeListModel *model = [[RechargeListModel alloc] initWithDict:rechargeDict];
                [_rechargeListArray addObject:model];
            }
            [self reChargeNumsView];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

//兑换次数
- (void)exchangeNumsRequestWithGoodId:(NSString *)goodsId Amount:(NSString *)amount {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"type":@"40",
                           @"cid":goodsId,
                           @"amount":amount
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kRechargePay];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            [self closeAddStarTipView];
            [self showTitle:responseObject[@"data"][@"message"]];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CallStarListRequest];
    [self setUpUI];
    self.navigationItem.title = @"呼唤明星";
}

#pragma mark -- UI
- (void)setUpUI {
    _callStarArray = [NSMutableArray arrayWithCapacity:1];
    _rechargeListArray = [NSMutableArray arrayWithCapacity:1];
    //tableView
    [self creatTableView];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - kNAV_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _callStarArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CallStarCell *cell = [CallStarCell loadNibCellWithTabelView:tableView];
    cell.starOrder.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
    cell.delegate = self;
    CallStarModel *model = _callStarArray[indexPath.row];
    cell.callStarModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.callStar = ^(){
        [self CallStarRequestWithShoutID:KString(model.sid) IndexPath:indexPath];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -- 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] init];
    UIImageView *topImgView = [[UIImageView alloc] init];
    topImgView.image = kSET_IMAGE(@"CallStarTopImage");
    [headView addSubview:topImgView];
    [topImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headView.top);
        make.bottom.mas_equalTo(headView.bottom);
        make.left.mas_equalTo(headView.left);
        make.right.mas_equalTo(headView.right);
    }];

    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 81*kScale;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

#pragma mark -- CallStarListDelegate
- (void)callStarLeavewordListWithCell:(CallStarCell *)callStarCell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:callStarCell];
    CallStarModel *model = _callStarArray[indexPath.row];
    CallStarLeavewordViewController *leaveWordVC = [[CallStarLeavewordViewController alloc] init];
    leaveWordVC.cid = model.sid;
    [self.navigationController pushViewController:leaveWordVC animated:YES];
}


#pragma mark -- 充值View
- (void)reChargeNumsView {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.addStarView = [[NSBundle mainBundle] loadNibNamed:@"AddStarView" owner:self options:nil].lastObject;
    self.addStarView.delegate = self;
    self.addStarView.balanceLabel.text = [NSString stringWithFormat:@"账户余额 : %@",KString(self.balance)] ;
    self.addStarView.dataArray = [NSArray arrayWithArray:_rechargeListArray];
    [window addSubview:self.addStarView];
    self.addStarView.backgroundColor = kRGB(0, 0, 0, 0.6);
    [self.addStarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
}

#pragma mark -- RechargeTipViewDelegate

- (void)closeAddStarTipView {
    [self.addStarView removeFromSuperview];
}

//兑换
- (void)exchangeNums {
    RechargeListModel *model = _rechargeListArray[self.tag];
    [self exchangeNumsRequestWithGoodId:model.cid Amount:model.c_value];
}
//选择类型
- (void)selectedTypeWithTag:(int)tag {
    self.tag = tag;
    switch (tag) {
        case 0:{
        }
            break;
        case 1:{
        }
            break;
        case 2:{
        }
            break;
        case 3:{
        }
            break;
            
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
